#coding=utf-8
from django.contrib.syndication.feeds import Feed
from lvproject.jj97.models import *
from django.contrib.syndication.feeds import FeedDoesNotExist
from django.utils.feedgenerator import Atom1Feed

class LatestNews(Feed):
    title = "最新闻!";
    link = "/jj97/"

    def items(self):
        return SiteNews.objects.order_by('-createdate')[:5]
    
    def item_link(self,obj):
        """
        Returns the URL for every item in the feed.
        """
        return 'http://localhost:8000/jj97/newsdetail/%d/'%(obj.id);

class NewsFeed(Feed):
    def get_object(self, bits):
        # In case of "/rss/beats/0613/foo/bar/baz/", or other such clutter,
        # check that bits has only one member.
        print len(bits);
        for bit in bits:
            print bit;
        if len(bits) != 1:
            raise ObjectDoesNotExist
        return SiteNews.objects.get(id=bits[0])

    def title(self, obj):
        return "unsins.com %s" % obj.shuttitle

    def link(self, obj):
        if not obj:
            raise FeedDoesNotExist
        
        return obj.get_absolute_url()
    
    def item_link(self,obj):
        """
        Returns the URL for every item in the feed.
        """
        return 'http://localhost:8000/jj97/newsdetail/%d/'%(obj.id);
    
    def description(self, obj):
        return "unsins.com %s" % obj.shuttitle

    def items(self, obj):
       return SiteNews.objects.filter(pk=obj.id).order_by('-createdate')[:30]


class RssSiteNewsFeed(Feed):
    title = "最新闻 RSS!";
    link = "/jj97/"
    description="unsins.com"

    def items(self):
        return SiteNews.objects.order_by('-createdate')[:5]
    
    def item_link(self,obj):
        """
        Returns the URL for every item in the feed.
        """
        return 'http://localhost:8000/jj97/newsdetail/%d/'%(obj.id);


class AtomSiteNewsFeed(RssSiteNewsFeed):
    title = "最新闻 ATOM!";
    link = "/jj97/"
    
    feed_type = Atom1Feed
    subtitle = RssSiteNewsFeed.description

class LatestNewsByCategory(Feed):
    pass

